package cn.stronglink.assetmanage.business.inspection.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * 维修资产列表
 * Created by zhengjinglan on 2018/6/20.
 */

public class RepairOrderAdapter extends RecyclerView.Adapter<RepairOrderAdapter.ViewHolder> implements View.OnClickListener{
    private Context mContext;
    private List<RepailDetail> datas;
    private onRecyclerListener listener;

    public RepairOrderAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public RepairOrderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_repair_order_list,parent ,false);
        view.setOnClickListener(this);
        return new RepairOrderAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RepairOrderAdapter.ViewHolder holder, int position) {
        RepailDetail repailDetail = datas.get(position);
        holder.bindRepo(repailDetail, position);
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {

        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(onRecyclerListener listener) {
        this.listener = listener;
    }

    public void notifyDatasChanged(List<RepailDetail> data) {
        this.datas = data;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClicked(v, (int)v.getTag());
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tv_repair_wait_asset_name;
        private TextView tv_repair_wait_sn;
        private TextView tv_repair_wait_bind_status;
        private LinearLayout ll_repair_asset_list;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_repair_wait_asset_name = (TextView) itemView.findViewById(R.id.tv_repair_wait_asset_name);
            tv_repair_wait_sn = (TextView) itemView.findViewById(R.id.tv_repair_wait_sn);
            tv_repair_wait_bind_status = (TextView) itemView.findViewById(R.id.tv_repair_wait_bind_status);
//            ll_repair_asset_list = (LinearLayout) itemView.findViewById(R.id.ll_repair_asset_list);
//            ll_repair_asset_list.setOnClickListener(this);
        }

        public void bindRepo(RepailDetail repailDetail, int position) {

            tv_repair_wait_asset_name.setText(repailDetail.getAssetName());
            tv_repair_wait_sn.setText(repailDetail.getSerialNo());
            int status = repailDetail.getStatus();
            if (status == Constants.STATUS_THREE){
                tv_repair_wait_bind_status.setText(mContext.getResources().getString(R.string.common_repair));
                tv_repair_wait_bind_status.setTextColor(mContext.getResources().getColor(R.color.bind_green));

            }else {
                tv_repair_wait_bind_status.setText(mContext.getResources().getString(R.string.common_unrepair));
                tv_repair_wait_bind_status.setTextColor(mContext.getResources().getColor(R.color.un_bind_red));
            }

        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(v, getAdapterPosition());
            }
        }
    }

}
